package com.allinpay.merchantweb.common.utils;
import java.math.BigDecimal;

/**
 * 从javascript脚本的加解密方法翻译，可以javascript加密，java解密，反之也可用
 * javascript 脚本摘自网络 ，略有变化
 * @author Simonyi Peng 2016-07-08
 * 
 * 
 * 关于 9007199254740992 的说明:
 * 
 * JavaScript中所有的数字，无论是整数还是小数，其类型均为Number。
 * 在程序内部，Number类型的实质是一个64位的浮点数，这与Java中double类型的浮点数是一致的；因此，JavaScript中所有的数都是浮点数。
 * 遵循IEEE 754标准（浮点数算术标准），JavaScript所能表示的数值范围为正负1.7976931348623157乘以10的308次方，其最小所能表示的小数为正负5乘以10的负324次方，这两个边界值可以分别通过访问Number对象的MAX_VALUE属性和MIN_VALUE属性来获取。
 * 对于整数，根据ECMAScript标准的要求(http://ecma262-5.com/ELS5_HTML.htm#Section_8.5)，
 * JavaScript能表示并进行精确算术运算的整数范围为：正负2的53次方，也即从最小值-9007199254740992到最大值+9007199254740992之间的范围；
 * 对于超过这个范围的整数，JavaScript依旧可以进行运算，但却不保证运算结果的精度。
 * 值得注意的是，对于整数的位运算（比如移位等操作），JavaScript仅支持32位整型数，也即从-2147483648到+2147483647之间的整数。
 *
 * 2016-08-02 对算法略做修改，增加arrayAdd函数，避免javascript大数相加的精度丢失问题
 * 
 * 2017-01-18 修改round函数bug
 * 
 * 2017-12-21 decrypt方法参数pwd必须为偶数
 */
public class SimpleScriptEncDec {
	
	public static String decrypt(String str,String pwd) {
		if(str == null || str.length() < 8) { 
			System.out.println("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted."); 
			return null;  
		} 
		if(pwd == null || pwd.length() <= 0) { 
			System.out.println("Please enter a password with which to decrypt the message."); 
			return null; 
		} 
		String prand = convert2PressOct(pwd); 
//System.out.println("_prand="+prand);		
		int sPos = (int)Math.floor(prand.length() / 5); 
//System.out.println("_sPos="+sPos);		
		int mult = Integer.parseInt("" + prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5)); 
//System.out.println("_mult="+mult);
		int incr = Math.round((pwd.length() % 2 ==0?(pwd.length()):(pwd.length()+1)) / 2); 
//System.out.println("_incr="+incr);
		double modu = Math.pow(2, 31) - 1; 
//System.out.println("_modu="+modu);		
		long salt = Integer.parseInt(str.substring(str.length() - 8, str.length()), 16); 

		str = str.substring(0, str.length() - 8); 
		prand += salt;
//System.out.println("_prand1="+prand);		

		while(prand.length() > 10) { 
			String t1 = prand.substring(0, 10).toString();
			String t2 = prand.substring(10, prand.length()).toString();
//			int t2length = t2.length();
//			if(t2length > 16){
//				String buffer = StringUtil.fillString("", '0', t2length-16, true);
//				t2 = t2.substring(0, 16) + buffer;
//			}else{
//				if(t2.compareTo("9007199254740992")>0 ) 
//					t2 = "9007199254740992";
//			}
			prand = new BigDecimal(t1).add(new BigDecimal(t2)).toString();
//			if(prand.length() > 16){
//				String buffer = StringUtil.fillString("", '0', t2length-16, true);
//				prand = prand.substring(0, 16) + buffer;
//			}else{
//				if(prand.compareTo("9007199254740992")>0 ) 
//					prand = "9007199254740992";
//			} 
		} 
//System.out.println("_prand2=" + prand);
		prand = new BigDecimal(mult).multiply(new BigDecimal(prand)).add(new BigDecimal(incr)).divideAndRemainder(new BigDecimal(modu))[1].toString();
//System.out.println("_prand3=" + prand);		

		int enc_chr = 0; 
		String enc_str = ""; 
		for(int i=0; i<str.length(); i+=2) { 
			int i1 = Integer.parseInt(str.substring(i, i+2), 16);
			BigDecimal i2 = new BigDecimal(prand).divide(new BigDecimal(modu),14,BigDecimal.ROUND_HALF_UP);
			BigDecimal i3 = i2.multiply(new BigDecimal(255));
			int i4 = (int)Math.floor(i3.doubleValue());
			enc_chr = i1 ^ i4;
			enc_str = enc_str + (char)enc_chr;
					
			prand = new BigDecimal(mult).multiply(new BigDecimal(prand)).add(new BigDecimal(incr)).divideAndRemainder(new BigDecimal(modu))[1].toString();
		} 
//System.out.println("_encStr=" + enc_str);
		return enc_str; 
	}

	
	public static String encrypt(String str, String pwd) { 
		if(pwd == null || pwd.length() <= 0) { 
			System.out.println("Please enter a password with which to encrypt the message."); 
			return null; 
		} 
		String prand = convert2PressOct(pwd); 
//System.out.println("prand=" + prand);
		int sPos = (int)Math.floor(prand.length() / 5); 
//System.out.println("sPos="+sPos);
		int mult = Integer.parseInt("" + prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5)); 
//System.out.println("mult="+mult);
		int incr = (int)Math.ceil(pwd.length() / 2); 
//System.out.println("incr="+incr);
		double modu = Math.pow(2, 31) - 1; 
//System.out.println("modu="+modu);		
		if(mult < 2) { 
			System.out.println("Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to choose a more complex or longer password."); 
			return null; 
		} 
		
		long salt = Math.round(Math.random() * 1000000000) % 100000000; 
//System.out.println("salt="+salt);
		prand += salt; 
//System.out.println("prand="+prand);		
		while(prand.length() > 10) {
			String t1 = prand.substring(0, 10).toString();
			String t2 = prand.substring(10, prand.length()).toString();
//			int t2length = t2.length();
//			if(t2length > 16){
//				String buffer = StringUtil.fillString("", '0', t2length-16, true);
//				t2 = t2.substring(0, 16) + buffer;
//			}else{
//				if(t2.compareTo("9007199254740992")>0 ) 
//					t2 = "9007199254740992";
//			}
			prand = new BigDecimal(t1).add(new BigDecimal(t2)).toString();
//			if(prand.length() > 16){
//				String buffer = StringUtil.fillString("", '0', t2length-16, true);
//				prand = prand.substring(0, 16) + buffer;
//			}else{
//				if(prand.compareTo("9007199254740992")>0 ) 
//					prand = "9007199254740992";
//			}
//			System.out.println("prandlist="+prand);		
		}
		prand = new BigDecimal(mult).multiply(new BigDecimal(prand)).add(new BigDecimal(incr)).divideAndRemainder(new BigDecimal(modu))[1].toString(); 
		int enc_chr = 0; 
		String enc_str = ""; 
		for(int i=0; i < str.length(); i++) { 
			BigDecimal b1 = new BigDecimal(prand);
			BigDecimal b2 = new BigDecimal(modu);
			BigDecimal b3 = b1.divide(b2, 14 ,BigDecimal.ROUND_HALF_UP);
			BigDecimal b4 = new BigDecimal(255);
			BigDecimal b5 = b3.multiply(b4);
			int tmpv = (int)(str.charAt(i)) ^ (int)Math.floor(b5.doubleValue());
			enc_chr = tmpv; 
			if(enc_chr < 16) { 
				enc_str += "0" + Integer.toHexString(enc_chr); 
			} else {
				enc_str += Integer.toHexString(enc_chr);				
			}
			prand = new BigDecimal(mult).multiply(new BigDecimal(prand)).add(new BigDecimal(incr)).divideAndRemainder(new BigDecimal(modu))[1].toString(); 
		} 
		String salt_str = Long.toHexString(salt);

		while(salt_str.length() < 8){
			salt_str = "0" + salt_str;
		}
		enc_str = enc_str + salt_str;
		return enc_str; 
	}

	
	/** 
	* 将字符串转成unicode的压缩十进制值 
	* @param str 待转字符串 
	* @return unicode字符串 
	*/ 
	public static String convert2PressOct(String str) { 
		str = (str == null ? "" : str); 
		String tmp = null;  
		StringBuffer sb = new StringBuffer(1000); 
		char c; int i, j; 
		sb.setLength(0); 
		for (i = 0; i < str.length(); i++)	{ 
			c = str.charAt(i);  
			j = (c >>>8); //取出高8位 
			tmp = Integer.toString(j); 
			if (tmp.length() == 1) 
				tmp = "0" + tmp;
			if(Integer.parseInt(tmp) != 0)
				sb.append(tmp); 
			j = (c & 0xFF); //取出低8位 
			tmp = Integer.toString(j); 
			if (tmp.length() == 1) 
				sb.append("0"); 
			sb.append(tmp); 	
		} 
		return (new String(sb)); 
	} 
	
	/** 
	* 将字符串转成unicode 
	* @param str 待转字符串 
	* @return unicode字符串 
	*/ 
	public static String convert(String str) { 
		str = (str == null ? "" : str); 
		String tmp = null;  
		StringBuffer sb = new StringBuffer(1000); 
		char c; int i, j; 
		sb.setLength(0); 
		for (i = 0; i < str.length(); i++)	{ 
			c = str.charAt(i); 
			sb.append("\\u"); 
			j = (c >>>8); //取出高8位 
			tmp = Integer.toHexString(j); 
			if (tmp.length() == 1) 
				sb.append("0"); 
			sb.append(tmp); 
			j = (c & 0xFF); //取出低8位 
			tmp = Integer.toHexString(j); 
			if (tmp.length() == 1) 
				sb.append("0"); 
			sb.append(tmp); 
	
		} 
		return (new String(sb)); 
	} 



	 
	/** 
	* 将unicode 转成字符串 
	* @param str 待转字符串 
	* @return 普通字符串 
	*/ 
	public static String revert(String str){ 
		str = (str == null ? "" : str); 
		if (str.indexOf("\\u") == -1)//如果不是unicode码则原样返回 
			return str; 
	
		StringBuffer sb = new StringBuffer(1000); 
	
		for (int i = 0; i < str.length() - 6;)	{ 
			String strTemp = str.substring(i, i + 6); 
			String value = strTemp.substring(2); 
			int c = 0; 
			for (int j = 0; j < value.length(); j++) { 
				char tempChar = value.charAt(j); 
				int t = 0; 
				switch (tempChar)	{ 
					case 'a': 
					t = 10; 
					break; 
					case 'b': 
					t = 11; 
					break; 
					case 'c': 
					t = 12; 
					break; 
					case 'd': 
					t = 13; 
					break; 
					case 'e': 
					t = 14; 
					break; 
					case 'f': 
					t = 15; 
					break; 
					default: 
					t = tempChar - 48; 
					break; 
				} 
				c += t * ((int) Math.pow(16, (value.length() - j - 1))); 
			} 
			sb.append((char) c); 
			i = i + 6; 
		} 
		return sb.toString(); 
	}
	
	public static void main(String[] args){
		//String src = "111111";
		//System.out.println(src);
		//String ret = SimpleScriptEncDec.encrypt(src, "201607221048461570");
		//System.out.println(ret);		
		String rst = SimpleScriptEncDec.decrypt("d3120ad2f97305823d36", "12345678");
		System.out.println("rst=["+rst+"]");
		
	}
}


/* javascript的加解密算法，与此处的java版本的加解密算法对应

 <SCRIPT LANGUAGE="JavaScript"> 
<!-- Begin 

function encrypt(str, pwd) { 
	if(pwd == null || pwd.length <= 0) { 
		alert("Please enter a password with which to encrypt the message."); 
		return null; 
	} 
	var prand = ""; 
	for(var i=0; i<pwd.length; i++) { 
		prand += pwd.charCodeAt(i).toString(); 
	} 
	
	var sPos = Math.floor(prand.length / 5); 
	var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5)); 
	var incr = Math.ceil(pwd.length / 2); 
	var modu = Math.pow(2, 31) - 1; 
	if(mult < 2) { 
		alert("Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to choose a more complex or longer password."); 
		return null; 
	} 
	
	var salt = Math.round(Math.random() * 1000000000) % 100000000; 
	prand += salt; 
	while(prand.length > 10) { 
		var x1 = prand.substring(0, 10);
		var x2 = prand.substring(10, prand.length);
		prand = arrayAdd(x1 + x2); 
	} 
	prand = (mult * prand + incr) % modu; 
	
	var enc_chr = ""; 
	var enc_str = ""; 
	for(var i=0; i<str.length; i++) { 
		enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255)); 
		if(enc_chr < 16) { 
			enc_str += "0" + enc_chr.toString(16); 
		} else 
			enc_str += enc_chr.toString(16); 
		prand = (mult * prand + incr) % modu; 
	} 
	
	salt = salt.toString(16); 
	while(salt.length < 8)
		salt = "0" + salt; 
	enc_str += salt; 
	
	return enc_str; 
} 

function decrypt(str, pwd) { 
	if(str == null || str.length < 8) { 
	alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted."); 
	return; 
	} 

	if(pwd == null || pwd.length <= 0) { 
	alert("Please enter a password with which to decrypt the message."); 
	return; 
	} 

	var prand = ""; 
	for(var i=0; i<pwd.length; i++) { 
		prand += pwd.charCodeAt(i).toString(); 
	} 

	var sPos = Math.floor(prand.length / 5); 
	var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5)); 
	var incr = Math.round(pwd.length / 2); 
	var modu = Math.pow(2, 31) - 1; 
	var salt = parseInt(str.substring(str.length - 8, str.length), 16); 
	str = str.substring(0, str.length - 8); 
	prand += salt; 

	while(prand.length > 10) { 
		var x1 = prand.substring(0, 10);
		var x2 = prand.substring(10, prand.length);
		prand = arrayAdd(x1 + x2); 
	} 
	prand = (mult * prand + incr) % modu; 

	var enc_chr = ""; 
	var enc_str = ""; 
	for(var i=0; i<str.length; i+=2) { 
		enc_chr = parseInt(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255)); 
		enc_str += String.fromCharCode(enc_chr); 
		prand = (mult * prand + incr) % modu; 
	} 

	return enc_str; 
} 


// 将两个字符串形式的大数转换为数组格式按位相加
// 用于避免javascript的精度丢失
function arrayAdd(str1,str2){
	var str1length = str1.length;
	var str2length = str2.length;
	var tmp = "";
	if(str1length != str2length){
		for(var i=0;i<Math.abs(str1length - str2length);i++){
			tmp = tmp + "0";
		}
	}//生成对较短字符串的补位串，保证有足够的位数进行相加
	var length = 0;
	if(str1length == str2length){
		length = str1length;
	}else{
		if (str1length < str2length){
			str1 = tmp + str1; length = str2length;
		}else{
			str2 = tmp + str2; length = str1length;	
		}
	}//完成短字符串的补位（前补0），同时得到两个字符串的长度
	
	var array1 = str1.split("");    //字符串转数组
	var array2 = str2.split("");	//字符串转数组
	var array3 = new Array(length + 1);    //用于存放加和值的数组，要比参数字符串长1个字节，用于保存可能的进位
	
	var add = 0;	//保存进位值的临时参数
	for(var i=length-1;i>=0;i--){  //从末位开始逐位相加
		var x1 = array1[i];
		var x2 = array2[i];
		var addvalue = parseInt(x1) + parseInt(x2) + parseInt(add)；
		if(addvalue >= 10){//有进位的情况
			if(addvalue >= 20){	//该分支不会出现			
				array3[i+1] = addvalue - 20;
				add = 2;
			}else{		//有进位则将进位值参数置为1，并将余数赋给array3的当前位
				array3[i+1] = addvalue - 10;
				add = 1;
			}
		}else{	//没有进位的情况 array3的值即位两值和进位值的和
			array3[i+1] = addvalue;
			add = 0; //进位值为0
		}
	}
	
	if(add > 0){//如果最终的进位值不为0，则赋给array3[0]作为最高位的值
		array3[0] = add; //注意，如果进位值为0，不要赋给array3[0]（初始化值为N/A），这样转出来的字符串才不会以0开头导致和java算法产生错位
	}
	return array3.join("");//数组转字符串

}

// End --> 
</script> 
 
 
*/


/*  

//本地测试用文件
<html>

<body>
<input type="text" id="src" value="112233"> enc by <input type="text" id="pwd"" value="188182096610"><br>
<input type="button" value="encrypt" onclick="javascript:enc()"> -> <input type="text" id='enc' value="">
<br/>
prand=<input type="text" id="prand"  value="" /><br>
sPos=<input type="text" id="sPos"   value="" /><br>
mult=<input type="text" id="mult"   value="" /><br>
incr=<input type="text" id="incr"   value="" /><br>
modu=<input type="text" id="modu"   value="" /><br>
salt=<input type="text" id="salt"   value="" /><br>
prand1=<input type="text" id="prand1" value="" /><br>
encStr=<input type="text" id="encStr" value="" /><br>
salt1=<input type="text" id="salt1"  value="" /><br>
end=<input type="text" id="end"    value="" /><br>

<hr/>

<input type="text" id='_enc' value=""> dec by <input type="text" id="pwd"" value="188182096610"><br>
<input type="button" value="decrypt" onclick="javascript:dec()"> -> <input type="text" id='dec' value="">
<br/>
_prand=<input type="text" id="_prand"  value="" /><br>
_sPos=<input type="text" id="_sPos"   value="" /><br>
_mult=<input type="text" id="_mult"   value="" /><br>
_incr=<input type="text" id="_incr"   value="" /><br>
_modu=<input type="text" id="_modu"   value="" /><br>
_salt=<input type="text" id="_salt"   value="" /><br>
_prand1=<input type="text" id="_prand1" value="" /><br>
_prand2=<input type="text" id="_prand2" value="" /><br>
_prand3=<input type="text" id="_prand3" value="" /><br>
_encStr=<input type="text" id="_encStr" value="" /><br>



</body>


<script language='javascript'>
function enc(){
	var src = document.getElementById('src').value;
	var pwd = document.getElementById('pwd').value;

	var rlt = encrypt(src,pwd);
	document.getElementById('enc').value = rlt;
	document.getElementById('_enc').value = rlt;
}

function dec(){
	var src = document.getElementById("enc").value;
	var pwd = document.getElementById("pwd").value;
	
	var rlt = decrypt(src,pwd);
	document.getElementById("dec").value = rlt;
}

function decrypt(str, pwd) { 
	if(str == null || str.length < 8) { 
	alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted."); 
	return; 
	} 

	if(pwd == null || pwd.length <= 0) { 
	alert("Please enter a password with which to decrypt the message."); 
	return; 
	} 

	var prand = ""; 
	for(var i=0; i<pwd.length; i++) { 
		prand += pwd.charCodeAt(i).toString(); 
	}
	
	document.getElementById("_prand").value = prand; 

	var sPos = Math.floor(prand.length / 5); 
	var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5)); 
	var incr = Math.round(pwd.length / 2); 
	var modu = Math.pow(2, 31) - 1; 
	var salt = parseInt(str.substring(str.length - 8, str.length), 16); 
	str = str.substring(0, str.length - 8); 
	prand += salt;
	
	document.getElementById("_sPos").value = sPos;
	document.getElementById("_mult").value = mult;
	document.getElementById("_incr").value = incr;
	document.getElementById("_modu").value = modu;
	document.getElementById("_salt").value = salt; 
	document.getElementById("_prand1").value = prand; 
	
	

	while(prand.length > 10) { 
		var x1 = prand.substring(0, 10);
		var x2 = prand.substring(10, prand.length);
		prand = arrayAdd(x1 , x2); 
	} 
	document.getElementById("_prand2").value = prand; 
	
	prand = (mult * prand + incr) % modu; 
	
	document.getElementById("_prand3").value = prand; 

	var enc_chr = ""; 
	var enc_str = ""; 
	for(var i=0; i<str.length; i+=2) { 
		enc_chr = parseInt(parseInt(str.substring(i, i+2), 16) ^ Math.floor((prand / modu) * 255)); 
		enc_str += String.fromCharCode(enc_chr); 
		prand = (mult * prand + incr) % modu; 
	} 
	
	document.getElementById("_encStr").value = enc_str; 

	return enc_str; 
} 



function encrypt(str, pwd) { 
	if(pwd == null || pwd.length <= 0) { 
		alert("Please enter a password with which to encrypt the message."); 
		return null; 
	} 
	var prand = ""; 
	for(var i=0; i<pwd.length; i++) { 
		prand += pwd.charCodeAt(i).toString(); 
	} 
	document.getElementById("prand").value=prand;
	
var sPos = Math.floor(prand.length / 5); 
var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos*2) + prand.charAt(sPos*3) + prand.charAt(sPos*4) + prand.charAt(sPos*5)); 
var incr = Math.ceil(pwd.length / 2); 
var modu = Math.pow(2, 31) - 1; 
if(mult < 2) { 
	alert("Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to choose a more complex or longer password."); 
	return null; 
} 
document.getElementById("sPos").value=sPos;
document.getElementById("mult").value=mult;
document.getElementById("incr").value=incr;
document.getElementById("modu").value=modu;


var salt = Math.round(Math.random() * 1000000000) % 100000000; 
document.getElementById("salt").value=salt;

prand += salt; 
while(prand.length > 10) { 
	var x1 = prand.substring(0, 10);
	var x2 = prand.substring(10, prand.length);
	prand = arrayAdd(x1 , x2); 
} 
prand = (mult * prand + incr) % modu; 

document.getElementById("prand1").value=prand;

var enc_chr = ""; 
var enc_str = ""; 
for(var i=0; i<str.length; i++) { 
	enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255)); 
	if(enc_chr < 16) { 
		enc_str += "0" + enc_chr.toString(16); 
	} else {
		enc_str += enc_chr.toString(16);
	}
	prand = (mult * prand + incr) % modu; 
} 
document.getElementById("encStr").value=enc_str;

salt = salt.toString(16); 
while(salt.length < 8)
	salt = "0" + salt; 
document.getElementById("salt1").value=salt;

enc_str += salt; 
document.getElementById("end").value=enc_str;



return enc_str; 
} 





function arrayAdd(str1,str2){

	var str1length = (""+str1).length;
	var str2length = (""+str2).length;

	var tmp = "";
	if(str1length != str2length){
		for(var i=0;i<Math.abs(str1length - str2length);i++){
			tmp = tmp + "0";
		}
	}//生成对较短字符串的补位串，保证有足够的位数进行相加
	var length = 0;
	if(str1length == str2length){
		length = str1length;
	}else{
		if (str1length < str2length){
			str1 = tmp + str1; length = str2length;
		}else{
			str2 = tmp + str2; length = str1length;	
		}
	}//完成短字符串的补位（前补0），同时得到两个字符串的长度
	
	var array1 = str1.split("");    //字符串转数组
	var array2 = str2.split("");	//字符串转数组
	var array3 = new Array(length + 1);    //用于存放加和值的数组，要比参数字符串长1个字节，用于保存可能的进位
	
	var add = 0;	//保存进位值的临时参数
	for(var i=length-1;i>=0;i--){  //从末位开始逐位相加
		var x1 = array1[i];
		var x2 = array2[i];
		var addvalue = parseInt(x1) + parseInt(x2) + parseInt(add);
		if(addvalue >= 10){//有进位的情况
			if(addvalue >= 20){	//该分支不会出现			
				array3[i+1] = addvalue - 20;
				add = 2;
			}else{		//有进位则将进位值参数置为1，并将余数赋给array3的当前位
				array3[i+1] = addvalue - 10;
				add = 1;
			}
		}else{	//没有进位的情况 array3的值即位两值和进位值的和
			array3[i+1] = addvalue;
			add = 0; //进位值为0
		}
	}
	
	if(add > 0){//如果最终的进位值不为0，则赋给array3[0]作为最高位的值
		array3[0] = add; //注意，如果进位值为0，不要赋给array3[0]（初始化值为N/A），这样转出来的字符串才不会以0开头导致和java算法产生错位
	}
	return array3.join("");//数组转字符串

}

</script>
</html>

*/